﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Security.Cryptography;
using System.IO;

namespace HashMe
{
    public static class DecryptTypes
    {
        public static String GetDecrypt(String DecryptType, String pTextToDecrypt, String pPassphrase)
        {

            Object[] parameter = { pTextToDecrypt, pPassphrase };
            MethodInfo mi = typeof(DecryptTypes).GetMethod("Get" + DecryptType + "Decrypt");
            String result = mi.Invoke(null, parameter).ToString();
            return result;
        }

        public static String GetRijndaelDecrypt(String pTextToDecrypt, String pPassphrase)
        {
            byte[] cipherBytes = Convert.FromBase64String(pTextToDecrypt);
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(pPassphrase, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            try
            {
                byte[] decryptedData = DecryptRijndaelString(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
                return System.Text.Encoding.Unicode.GetString(decryptedData);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        private static byte[] DecryptRijndaelString(byte[] cipherData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            Rijndael alg = RijndaelManaged.Create();
            alg.Key = Key;
            alg.IV = IV;
            CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(cipherData, 0, cipherData.Length);
            cs.Close();
            byte[] decryptedData = ms.ToArray();
            return decryptedData;
        }

    }
}
